/*@author cerimice */
package cerimice;

import java.util.Collection;

import com.vaadin.ui.ComboBox;

public class ComboLista extends ComboBox
    {
    public ComboLista()
        {super();}
    
    public ComboLista(String caption)
        {super(caption);}
    
    public ComboLista(String caption, Collection<?> opciones)
        {super(caption, opciones);}
    
    public void setSelectedKey(String key)
        {
        Object seleccion = null;
        Collection objs = getItemIds();
        for(Object obj:objs)
            {
            if(obj.toString().split(" - ")[0].equals(key))
                {seleccion = obj;}
            }
        if(seleccion != null)
            {this.setValue(seleccion);}
        }
    
    public String getSelectedValue()
        {
        String valor = this.getValue().toString();
        if(valor==null){return "";}
        if(valor.equals("null")){return "";}
        return valor.split(" - ")[1];
        }
    
    public String getSelectedKey()
        {
        String valor = this.getValue().toString();
        if(valor==null){return "";}
        if(valor.equals("null")){return "";}
        return valor.split(" - ")[0];
        }
    
    public void addListItems(Collection items)
        {
        this.unselect(items);
        this.removeAllItems();
        for(Object obj:items)
            {this.addItem(obj);}
        }
    }
